package com.augurit.gzcgwsp.userdetail.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.system.service.org.IAcMenuService;
import com.augurit.common.system.service.org.IOmUserService;
import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.system.web.org.form.AtHomepageConfigForm;
import com.augurit.common.system.web.org.form.OmUserForm;
import com.augurit.gzcgwsp.sendNotice.service.IUserForFxService;

@SuppressWarnings("serial")
@Results({
	@Result(name = "index", location = "/WEB-INF/content/index/index-treetab_new.jsp"),
})
public class PsIndexAction  extends ExtCrudActionSupport<OmUserForm>  {
	@Autowired
	private IAcMenuService acMenuService;
	@Autowired
	private IOmUserService omUserService;
	@Autowired
	private IUserForFxService userForFxService;
	private OmUserForm form;
	// 属性
	private Long id; 	
	public IAcMenuService getAcMenuService() {
		return acMenuService;
	}
	public void setAcMenuService(IAcMenuService acMenuService) {
		this.acMenuService = acMenuService;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String index()throws Exception{
		
		return "index";
	}
	public String modifyPwd()throws Exception{
		String msg="";
		form= omUserService.get(this.getLoginUserName());
		//md5加密密码
		String oldPwd=org.hsqldb.lib.MD5.encodeString(this.getParameter("old_password"), "UTF-8");
		String newPwd=org.hsqldb.lib.MD5.encodeString(this.getParameter("new_password"), "UTF-8");
		if(oldPwd.equals(form.getPassword())){
			form.setPassword(newPwd);
			omUserService.save(form);
			userForFxService.updateUserPassword(form.getUserCode(),oldPwd,newPwd);//同步修改用户密码
			msg = "{success:true,message:true}";
		}else{
			msg = "{success:true,message:false}";
		}
		this.getResponse().getWriter().print(msg);
		return this.NONE;
	}
	public String findUser() throws Exception {
		OmUserForm form= omUserService.get(this.getLoginUserName());
		this.extRenderFormJson(form);
		return this.NONE;
	}
	public String save() throws Exception {
		String userName=this.getParameter("userName");
		String title=this.getParameter("title");
		String sex=this.getParameter("sex");
		String officeTel=this.getParameter("officeTel");
		String mobile=this.getParameter("mobile");
		String msn=this.getParameter("msn");
		String qq=this.getParameter("qq");
		String email=this.getParameter("email");
		String remark=this.getParameter("remark");
		String userId=this.getParameter("userId");
		form=omUserService.get(Long.parseLong(userId));
		form.setUserName(userName);
		form.setTitle(title);
		form.setSex(sex);
		form.setOfficeTel(officeTel);
		form.setMobile(mobile);
		form.setMsn(msn);
		form.setQq(qq);
		form.setEmail(email);
		form.setRemark(remark);
		omUserService.save(form);
		userForFxService.updateUser(form, null, null);//同步修改用户信息
		this.extRenderSuccess();
		return this.NONE;
	}
	/**
	 * 获取指定节点的子节点列表
	 */
	public String getChildren() throws Exception {
		List children = acMenuService.getChildTree(this.getLoginUser(), id, 1, this.getHomepageConfig().getModelName(), true);
		extRenderListJson(children, false);
		return null;
	}
	/**
	 * 获取系统首页配置信息
	 */
	public AtHomepageConfigForm getHomepageConfig(){
		return WebContextUtils.getAtHomepageConfigBuffer(this.getApplication());
	}
	
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		
	}
	public OmUserForm getModel() {
		// TODO Auto-generated method stub
		return form;
	}

}
